package com.ymatou.iapi.login.testcase;

import java.io.IOException;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.auth.parameter.SignAuthBean;
import com.ymatou.iapi.login.parameter.SellerLoginBean;
import com.ymatou.iapi.login.parameter.WxUserLoginBean;
import com.ymatou.iapi.login.service.SellerLoginCall;
import com.ymatou.iapi.userregister.parameter.SellerRegisterBean;
import com.ymatou.iapi.userservice.parameter.enums.ThirdPartyTypeEnum;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.LiveShopUserCallService;
import com.ymttest.database.model.LoginLog;
import com.ymttest.database.model.ThirdpartyUserbind;
import com.ymttest.database.model.ThirdpartyUserbindHistory;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
/**
*直播微店买手登入
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SellerLogin{
	private static SellerLoginBean sellerloginBean;
	private static SellerLoginCall sellerloginCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播微店买手登入");
	}

	@Before
	public void caseUp() {
		sellerloginBean=new SellerLoginBean();
		sellerloginCall=new SellerLoginCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyToken() throws ClientProtocolException, IOException, InterruptedException {
		JsonObject result = sellerloginCall.getJsonObject().get("data").getAsJsonObject();
		String token=result.get("token").getAsString();
		Thread.sleep(500);
		SignAuthBean sab=new SignAuthBean();
		sab.setToken(token);
		Integer suid=JUserCallService.signAuth(sab).getUserId();
		Logger.verifyEquals(suid, sellerloginCall.getUserid(), "验证Token对应的uid");
	}
	private void verifyDB() throws InterruptedException {
		int uid= sellerloginCall.getUserid();
		LoginLog loginLog = new JUserWapper().selectLastLoginLogByUid(uid).get(0);
		Logger.debug(FormatUtil.beanToGSONString(loginLog));
		Logger.verifyEquals(sellerloginCall.getJsonObject().get("data").getAsJsonObject().get("loginId").getAsString(), loginLog.getLoginId(), "验证LoginLog表中LoginId");
		Logger.verifyEquals(sellerloginBean.getClientIp(), loginLog.getClientIp(), "验证LoginLog表中ClientIp");
		// 码头登陆的为0
		Logger.verifyEquals(0, loginLog.getChannelSource(), "验证LoginLog表中ChannelSource");
		Logger.verifyEquals(sellerloginBean.getTerminalSource(), loginLog.getTerminalSource(), "验证LoginLog表中TerminalSource");
		Logger.verifyEquals(sellerloginBean.getLid(), loginLog.getDeviceId(), "验证LoginLog表中DeviceId");
		Logger.verifyNotNull(loginLog.getServerIP(), "验证LoginLog表中ServerIP");

		if (sellerloginCall.getString("code").equals("200")) {
			Logger.verifyIsContains("成功", loginLog.getResponseDesc(), "验证LoginLog表中ResponseDesc");
			Logger.verifyEquals(200, loginLog.getResponseCode(), "验证LoginLog表中ResponseCode");
		} 
		Logger.verifyEquals(1, loginLog.getUserSystem(), "验证LoginLog表中UserSystem");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerLogin_001() {
		Logger.start(true,"直播微店买手登入成功");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(200, sellerloginCall.getCode(), "验证code");
			Logger.verifyEquals(js.get("userId"), sellerloginCall.getUserid(), "验证userid");
			verifyToken();
			 verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_002() {
		Logger.start(true,"不输入loginMobile失败");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setLoginMobile(null);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(400, sellerloginCall.getCode(), "验证code");
			Logger.verifyIsContains("手机号必须填写", sellerloginCall.getJsonObject().get("message").getAsString(), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_003() {
		Logger.start(true,"不输入regSource,失败");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setRegSource(null);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(400, sellerloginCall.getCode(), "验证code");
			Logger.verifyIsContains("注册来源必须填写", sellerloginCall.getJsonObject().get("message").getAsString(), "验证message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_004() {
		Logger.start(true,"不输入terminalSource,失败");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setTerminalSource(null);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(400, sellerloginCall.getCode(), "验证code");
			Logger.verifyIsContains("终端来源必须填写", sellerloginCall.getJsonObject().get("message").getAsString(), "验证message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_005() {
		Logger.start(true,"不输入clientIp,失败");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setClientIp(null);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(400, sellerloginCall.getCode(), "验证code");
			Logger.verifyIsContains("客户端Ip", sellerloginCall.getJsonObject().get("message").getAsString(), "验证message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_006() {
		Logger.start(true,"passwordType0,直播微店买手登入成功");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setPasswordType(0);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(200, sellerloginCall.getCode(), "验证code");
			Logger.verifyEquals(js.get("userId"), sellerloginCall.getUserid(), "验证userid");
			verifyToken();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_007() {
		Logger.start(true,"passwordType1,直播微店买手登入成功");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			Thread.sleep(500);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setPasswordType(1);
			String pwd = DesUtil.encrypt(sellerloginBean.getPassword(),DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
			sellerloginBean.setPassword(pwd);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(200, sellerloginCall.getCode(), "验证code");
			Logger.verifyEquals(js.get("userId"), sellerloginCall.getUserid(), "验证userid");
			verifyToken();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_008() {
		Logger.start(true,"passwordType1,使用未加密密码直播微店买手登入失败");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setPasswordType(1);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(110, sellerloginCall.getCode(), "验证code");
			Logger.verifyEquals("密码验证失败", sellerloginCall.getJsonObject().get("message").getAsString(), "验证message");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_009() {
		Logger.start(true,"passwordType2,直播微店买手登入成功,RSA加密登入");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setPasswordType(2);
			byte[] rsabytes = RSAUtils.encryptByPublicKey(srb.getPassword().getBytes(),
					(RSAPublicKey) RSAUtils.loadPublicKey(EnvSetup
							.getData("USER_PUBLIC_KEY")));
			String pasbase64 = Base64.getEncoder().encodeToString(rsabytes);
			sellerloginBean.setPassword(pasbase64);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(200, sellerloginCall.getCode(), "验证code");
			Logger.verifyEquals(js.get("userId"), sellerloginCall.getUserid(), "验证userid");
			verifyToken();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLogin_010() {
		Logger.start(true,"passwordType2RSA加密登入,密码错误,直播微店买手登入失败");
		try {
			SellerRegisterBean srb=LiveShopUserCallService.initSellerReg();
			JSONObject js = LiveShopUserCallService.LiveShopSellerRegister(srb);
			sellerloginBean=LiveShopUserCallService.initSellerlogin(srb);
			sellerloginBean.setPasswordType(2);
			byte[] rsabytes = RSAUtils.encryptByPublicKey((srb.getPassword()+1).getBytes(),
					(RSAPublicKey) RSAUtils.loadPublicKey(EnvSetup
							.getData("USER_PUBLIC_KEY")));
			String pasbase64 = Base64.getEncoder().encodeToString(rsabytes);
			sellerloginBean.setPassword(pasbase64);
			sellerloginCall.setData(sellerloginBean);
			sellerloginCall.callService();			
			Logger.verifyEquals(110, sellerloginCall.getCode(), "验证code");
//			Logger.verifyEquals(js.get("userId"), sellerloginCall.getUserid(), "验证userid");
//			verifyToken();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}